package com.billdowney.webconfig;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

import com.billdowney.listener.ContentListener;
import com.billdowney.service.util.project.SystemAppConst;
import com.billdowney.util.file.FileUtil;

/**
 * spring Boot配置
 * 
 * @author 超级小富翁 - BillDowney
 * @date 2019年5月7日 上午10:20:49
 */
@Configuration
public class SpringBootConfig {

	private Logger logger = LoggerFactory.getLogger(ContentListener.class);

	/**
	 * 存放需要加载文件变量的文件
	 */
	private final String LOAD_FILE_PATH = "application.yml";
	/**
	 * 路径前缀
	 */
	private final String PATH_PREFIX = "classpath:";
	/**
	 * properties文件后缀
	 */
	private final String FILE_PROPERTIES_SUFFIX = "properties";
	/**
	 * yaml文件后缀
	 */
	private final String FILE_YAML_SUFFIX = "yml";

	/**
	 * 初始化加载需要的yaml文件
	 * 
	 * @author: 超级小富翁 - BillDowney
	 * @throws IOException 
	 * @date: 2019年5月7日 下午12:41:39
	 */
	@Bean
	public PropertySourcesPlaceholderConfigurer properties() throws IOException {
		// 先加载配置文件，读取里面的属性
		YamlPropertiesFactoryBean applicationYaml = new YamlPropertiesFactoryBean();
		applicationYaml.setResources(new ClassPathResource(LOAD_FILE_PATH));
		List<String> initLoadFiles = this.getInitLoadFiles(applicationYaml.getObject());
		// 开始加载需要的文件
		PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
		List<Resource> listProperties = new ArrayList<>();
		List<Resource> ymlProperties = new ArrayList<>();
		for (int i = 0; i < initLoadFiles.size(); i++) {
			Resource resource = null;
			if (initLoadFiles.get(i).startsWith(PATH_PREFIX)) {
				resource = new ClassPathResource(initLoadFiles.get(i).replaceFirst(PATH_PREFIX, ""));
			} else {
				resource = new FileSystemResource(initLoadFiles.get(i));
			}
			switch (FileUtil.getFileSuffix(resource.getFilename())) {
			case FILE_PROPERTIES_SUFFIX:
				listProperties.add(resource);
				break;
			case FILE_YAML_SUFFIX:
				ymlProperties.add(resource);
				break;
			default:
				logger.warn("初始化加载的文件类型不能识别：" + resource.getFilename());
				break;
			}
		}
		PropertiesFactoryBean prop = new PropertiesFactoryBean();
		prop.setLocations(listProperties.toArray(new Resource[listProperties.size()]));
		prop.afterPropertiesSet();
		YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
		yaml.setResources(ymlProperties.toArray(new Resource[ymlProperties.size()]));
		configurer.setPropertiesArray(prop.getObject(), yaml.getObject());
		return configurer;
	}

	/**
	 * 从application*.yml中获取需要加载的文件列表
	 * 
	 * @param properties
	 * @return 获取到的文件列表
	 * @author: 超级小富翁 - BillDowney
	 * @date: 2019年5月13日 下午4:41:59
	 */
	private List<String> getInitLoadFiles(Properties properties) {
		List<String> list = new ArrayList<>();
		String line;
		for (int i = 0; (line = properties.getProperty(SystemAppConst.initLoadFiles + "[" + i + "]")) != null; i++) {
			list.add(line);
		}
		return list;
	}

}
